<script>
// APP 和 小程序平台
// #ifdef  APP-PLUS || MP-WEIXIN
import TIM from 'tim-wx-sdk'
import COS from 'cos-wx-sdk-v5'
// #endif

// #ifdef H5
import TIM from 'tim-js-sdk'
import TIMUploadPlugin from 'tim-upload-plugin'
console.log(' TUIKit 暂不支持 H5 / web ，请使用者自己完成兼容哦，页面刷新可能会导致报错，需要重新登录 ')
// #endif

// #ifdef APP-PLUS
import Aegis from 'aegis-weex-sdk'
// #endif

// #ifdef MP-WEIXIN
import Aegis from 'aegis-mp-sdk'
// #endif

// #ifdef H5
import Aegis from 'aegis-web-sdk'
// #endif

import logger from './utils/logger' // app.js
import { genTestUserSig } from './debug/GenerateTestUserSig.js'

const aegis = new Aegis({
	id: 'iHWefAYqKznuxWjLnr', // 项目key
	reportApiSpeed: true // 接口测速
})

uni.$aegis = aegis
// 首先需要通过 uni.requireNativePlugin("ModuleName") 获取 module
// #ifdef APP-PLUS
const TUICalling = uni.requireNativePlugin('TUICallingUniPlugin-TUICallingModule')
console.log(`| app |  TUICallingUniPlugin-TUICallingModule | TUICalling: ${TUICalling}`)
if (typeof TUICalling == 'undefined') {
	logger.error(
		'如果需要音视频功能，请集成原生插件，使用真机运行并且自定义基座调试哦～ 插件地址：https://ext.dcloud.net.cn/plugin?id=7097 , 调试地址：https://nativesupport.dcloud.net.cn/NativePlugin/use/use'
	)
}
// #endif

import store from '@/store/index.js'
export default {
	onLaunch: function(o) {
		const SDKAppID = genTestUserSig('userIdwxopenId001').sdkAppID
		uni.$aegis.reportEvent({
			name: 'onLaunch',
			ext1: 'onLaunch-success',
			ext2: 'uniTuikitExternal',
			ext3: `${SDKAppID}`
		})
		uni.setStorageSync(`TIM_${SDKAppID}_isTUIKit`, true)
		// 重点注意： 为了 uni-app 更好地接入使用 tim，快速定位和解决问题，请勿修改 uni.$TUIKit 命名。
		// 如果您已经接入 tim ，请将 uni.tim 修改为 uni.$TUIKit。
		uni.$TUIKit = TIM.create({
			SDKAppID: SDKAppID
		})
		// #ifndef H5
		uni.$TUIKit.registerPlugin({
			'cos-wx-sdk': COS
		})
		// #endif

		// #ifdef H5
		uni.$TUIKit.registerPlugin({ 'tim-upload-plugin': TIMUploadPlugin })
		// #endif
		// 将原生插件挂载在 uni 上
		// #ifdef APP-PLUS
		uni.$TUICalling = TUICalling
		// #endif
		// 如果您已创建了 tim，请将 tim 实例挂载在 wx 上，且不可以修改 wx.$TIM（修改变量可能导致 TUICalling 组件无法正常使用）, 完成 TUICalling 初始化，
		// 如果您没有创建，可以不传
		// #ifdef MP-WEIXIN
		wx.$TIM = uni.$TUIKit
		// #endif
		uni.$TUIKitTIM = TIM
		uni.$TUIKitEvent = TIM.EVENT
		uni.$TUIKitVersion = TIM.VERSION
		uni.$TUIKitTypes = TIM.TYPES // 监听系统级事件
		uni.$resetLoginData = this.resetLoginData()
		uni.$TUIKit.on(uni.$TUIKitEvent.SDK_READY, this.onSDKReady)
		uni.$TUIKit.on(uni.$TUIKitEvent.SDK_NOT_READY, this.onSdkNotReady)
		uni.$TUIKit.on(uni.$TUIKitEvent.KICKED_OUT, this.onKickedOut)
		uni.$TUIKit.on(uni.$TUIKitEvent.ERROR, this.onTIMError)
		uni.$TUIKit.on(uni.$TUIKitEvent.NET_STATE_CHANGE, this.onNetStateChange)
		uni.$TUIKit.on(uni.$TUIKitEvent.SDK_RELOAD, this.onSDKReload)

		// 冷启动时在该处获取参数,将参数传递到globalData全局对象下
		console.log('this.globalData.query', o.query)
		this.globalData.query = o.query || {}
		// 小程序跳小程序有的同学会使用extraData
		if (o.referrerInfo) {
			this.globalData.query = Object.assign(this.globalData.query, o.referrerInfo.extraData)
		}
		// 进入携带查询参数
		if (this.globalData.query) {
			// #ifdef MP-WEIXIN
			// 获取微信分享用户id
			if (this.globalData.query.id) {
				// 分享获取
				this.globalData.shareUserId = this.globalData.query.id
			} else {
				// 二维码获取
				const scene = decodeURIComponent(this.globalData.query.scene)
				const userId = scene && scene.split('=')[1]
				if (userId) {
					this.globalData.shareUserId = userId
				}
			}
			console.log('邀请人ID:' + this.globalData.shareUserId)
			// #endif

			// #ifdef MP-ALIPAY
			//拿到用户通过二维码传递过来的用户id
			this.globalData.shareUserId = this.globalData.query.userId
			// #endif
		}
		this.inviteResolve()
		this.$AppEntryController.getWeChatOpenId()
		// #ifdef H5
		this.$AppEntryController.handleH5BrowserAddressBarAuth()
		// #endif
	},
	globalData: {
		// userInfo: userID userSig token phone
		userInfo: null,
		// 个人信息
		userProfile: null,
		isTUIKit: true,
		headerHeight: 0,
		statusBarHeight: 0,
		SDKAppID: genTestUserSig('').sdkAppID,
		// 分享用户ID
		shareUserId: '',
		query: {}
	},
	methods: {
		// 邀请判断
		inviteResolve() {
			console.log('this.userInfo', store.state.userInfo)
			if (!store.state.userInfo) {
				console.log('没有用户信息')
				return
			}
			if (!this.globalData.shareUserId) {
				console.log('没有邀请ID，不需要走下面流程了')
				return
			}
			var param = {
				userid: store.state.userInfo.id,
				openid: store.state.userInfo.openid,
				inviterUserid: this.globalData.shareUserId
			}
			this.$apis.inviteJudge(param)
		},
		// TODO:
		resetLoginData() {
			this.globalData.expiresIn = ''
			this.globalData.sessionID = ''
			this.globalData.userInfo = {
				userID: '',
				userSig: '',
				token: '',
				phone: ''
			}
			this.globalData.userProfile = null
			logger.log(`| app |  resetLoginData | globalData: ${this.globalData}`)
		},
		onTIMError() {},
		onSDKReady({ name }) {
			const isSDKReady = name === uni.$TUIKitEvent.SDK_READY ? true : false
			uni.$emit('isSDKReady', {
				isSDKReady: true
			})
		},
		onNetStateChange() {},
		onSDKReload() {},
		onSdkNotReady() {},
		onKickedOut() {
			uni.showToast({
				title: '您被踢下线',
				icon: 'error'
			})
			uni.navigateTo({
				url: './pages/TUI-Login/login'
			})
		}
	},
	onShow: function(o) {
		//console.log('App Show', o);
		uni.setStorageSync('shareInfo', o) //分享后的信息
	},
	onHide: function() {
		//console.log('App Hide');
	}
}
</script>

<style lang="scss">
@import './common/yzb-icon.css';
/*每个页面公共css */
@import './common/css/basic.scss';
@import './common/css/iconfont.css';
@import './common/css/iconfont2.css';
@import 'uview-ui/index.scss';
/*通用 */
view {
	// display: flex;
	font-size: $uni-font-size-base;
}

progress,
checkbox-group {
	width: 100%;
}

form {
	width: 100%;
}

/**
	 * 全局输入框文本大小
	 */
input {
	font-size: $uni-font-size-lg;
}

/**
	 * 全局文本大小
	 */
text {
	font-size: $uni-font-size-base;
}

/**
	 * 内容区域
	 */
.content {
	flex: 1;
	width: 100%;
	height: 100%;
	flex-direction: column;
	align-items: center;
}

/**
	 * 横向布局
	 */
.row {
	display: flex;
	flex-direction: row;
}

/**
	 * 竖向布局
	 */
.column {
	display: flex;
	flex-direction: column;
}

/**
	 * 居中对齐
	 */
.center-algin {
	display: flex;
	justify-content: center;
	align-items: center;
}

.align-height {
	align-items: center;
}

.align-width {
	justify-content: center;
}
.align-top {
	align-items: flex-start;
}
.align-bottom {
	align-items: flex-end;
}

/**
	 * 左右对齐
	 */
.space-between-algin {
	width: 100%;
	justify-content: space-between;
	align-items: center;
}

/* 文本溢出隐藏 */
.text-ellipsis {
	overflow: hidden;
	white-space: nowrap;
	text-overflow: ellipsis;
}

/* 背景色 */
.bg-white {
	background: #ffffff;
}

.text-color {
	color: $uni-text-color;
}
.text-color-inverse {
	color: $uni-text-color-inverse;
}
.text-color-grey {
	color: $uni-text-color-grey;
}
.text-color-placeholder {
	color: $uni-text-color-placeholder;
}
.text-color-disable {
	color: $uni-text-color-disable;
}

.text-size-base {
	font-size: $uni-font-size-base;
}
.text-size-lg {
	font-size: $uni-font-size-lg;
}
.text-size-40 {
	font-size: $font-size-40;
}
/**
	 *  字体加粗
	 */
.font-bold {
	font-weight: bold;
}

/**
	 * 1行显示，多与的后面省略号显示
	 */
.ellipsis-oneline {
	text-overflow: -o-ellipsis-lastline;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-line-clamp: 1;
	-webkit-box-orient: vertical;
}

/**
	 * 两行显示，多与的后面省略号显示
	 */
.ellipsis-twoline {
	text-overflow: -o-ellipsis-lastline;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-line-clamp: 2;
	-webkit-box-orient: vertical;
}

.bottom-line {
	border-bottom: $border-color-base solid 1upx;
}

.left-line {
	border-left: $border-color-base solid 1upx;
}
.right-line {
	border-right: $border-color-base solid 1upx;
}

.top-line {
	border-top: $border-color-base solid 1upx;
}

.width-100 {
	width: 100%;
}
.height-100 {
	height: 100%;
}

.padding-5 {
	padding: 5upx;
}
.padding-10 {
	padding: 10upx;
}
.padding-15 {
	padding: 15upx;
}
.padding-20 {
	padding: 20upx;
}
.padding-height-5 {
	padding: 5upx 0;
}
.padding-height-10 {
	padding: 10upx 0;
}
.padding-height-15 {
	padding: 15upx 0;
}
.padding-height-20 {
	padding: 20upx 0;
}
.padding-height-25 {
	padding: 25upx 0;
}
.padding-height-30 {
	padding: 30upx 0;
}
.padding-height-40 {
	padding: 40upx 0;
}
.padding-width-5 {
	padding: 0 5upx;
}
.padding-width-10 {
	padding: 0 10upx;
}
.padding-width-15 {
	padding: 0 15upx;
}
.padding-width-20 {
	padding: 0 20upx;
}
.margin-5 {
	margin: 5upx;
}
.margin-10 {
	margin: 10upx;
}
.margin-15 {
	margin: 15upx;
}
.margin-20 {
	margin: 20upx;
}
.margin-left-5 {
	margin-left: 5upx;
}
.margin-left-10 {
	margin-left: 10upx;
}
.margin-left-15 {
	margin-left: 15upx;
}
.margin-left-20 {
	margin-left: 20upx;
}
.margin-right-5 {
	margin-right: 5upx;
}
.margin-right-10 {
	margin-right: 10upx;
}
.margin-right-15 {
	margin-right: 15upx;
}
.margin-right-20 {
	margin-right: 20upx;
}
.margin-top-5 {
	margin-top: 5upx;
}
.margin-top-10 {
	margin-top: 10upx;
}
.margin-top-15 {
	margin-top: 15upx;
}
.margin-top-20 {
	margin-top: 20upx;
}
.margin-bottom-5 {
	margin-bottom: 5upx;
}
.margin-bottom-10 {
	margin-bottom: 10upx;
}
.margin-bottom-15 {
	margin-bottom: 15upx;
}
.margin-bottom-20 {
	margin-bottom: 20upx;
}
.margin-height-5 {
	margin: 5upx 0;
}
.margin-height-10 {
	margin: 10upx 0;
}
.margin-height-15 {
	margin: 15upx 0;
}
.margin-height-20 {
	margin: 20upx 0;
}
.margin-width-5 {
	margin: 0 5upx;
}
.margin-width-10 {
	margin: 0 10upx;
}
.margin-width-15 {
	margin: 0 15upx;
}
.margin-width-20 {
	margin: 0 20upx;
}
.u-search__action {
	width: 170upx !important;
	height: 80upx;
	background: #7bc2f8;
	font-size: 32upx !important;
	font-family: Source Han Sans CN-Regular, Source Han Sans CN;
	font-weight: 400;
	color: #ffffff !important;
	border-radius: 50upx;
	line-height: 80upx;
	z-index: 10000;
}

.titleLocation {
	position: fixed;
	top: 0;
	left: 0;
	z-index: 1;
	width: 100%;
	background: #fff;
}
</style>
